﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IDchecker
{
    public partial class Form1 : Form
    {
        public string IDnum { get; private set; }
        public string p = @"^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$";


public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            IDnum = textBox1.Text;
            if (Regex.IsMatch(IDnum, p))
            {
                textBox2.Text = "真";
            }
            else
            {
                textBox2.Text = "假";
                textBox3.Text = "还有检验的必要吗（恼";
                textBox4.Text = "假";


            }

            int[] factor = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            char[] parity = { '1', '0', 'X', '9', '8', '7', '6' ,'5','4','3','2'};
            
            if (textBox2.Text == "真")
            {
                var sum = 0;
                for (var i = 0; i < 17; i++)
                {
                    sum += (IDnum[i]-48) * factor[i];
                }
                if (parity[sum % 11] == IDnum[17])
                {
                    textBox3.Text = "真";
                    textBox4.Text = "真";
                }
                else
                {
                    textBox3.Text = "假";
                    textBox4.Text = "假";
                }
            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
